package com.msb.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.msb.crm.base.BaseService;
import com.msb.crm.dao.CusDevPlanMapper;
import com.msb.crm.dao.SaleChanceMapper;
import com.msb.crm.query.CusDevPlanQuery;
import com.msb.crm.query.SaleChanceQuery;
import com.msb.crm.utils.AssertUtil;
import com.msb.crm.vo.CusDevPlan;
import com.msb.crm.vo.SaleChance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CusDevPlanService extends BaseService<CusDevPlan,Integer> {

    @Autowired
    private CusDevPlanMapper cusDevPlanMapper;

    @Autowired
    private SaleChanceMapper saleChanceMapper;


    /**
     * 客户开发计划查询
     * @param cusDevPlanQuery
     * @return
     */
    public Map<String,Object> queryCusDevPlanParams(CusDevPlanQuery cusDevPlanQuery){

        Map<String,Object> map = new HashMap<>();

        //开启分页
        PageHelper.startPage(cusDevPlanQuery.getPage(), cusDevPlanQuery.getLimit());

        //得到对应分页的对象
        PageInfo<CusDevPlan> pageInfo = new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));

        //设置map对象
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());

        //设置分页好的列表
        map.put("data",pageInfo.getList());

        return map;
    }


    /**
     * 添加计划
     * @param cusDevPlan
     */
    @Transactional
    public void addCusDevPlan(CusDevPlan cusDevPlan){
        /*1.参数校验*/
        checkCusDevPlanParams(cusDevPlan);

        /*2.设置参数的默认值*/
        // 是否有效  默认有效
        cusDevPlan.setIsValid(1);
        //创建时间  系统当前时间
        cusDevPlan.setCreateDate(new Date());
        //修改时间  系统当前时间
        cusDevPlan.setUpdateDate(new Date());

        /*3.执行添加操纵，判断受影响的行数*/
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan)!=1,"计划项数据添加失败！");
    }


    /**
     * 更新计划项
     * @param cusDevPlan
     */
    @Transactional
    public void updateCusDevPlan(CusDevPlan cusDevPlan){
        /*1.参数校验*/
        AssertUtil.isTrue(null == cusDevPlan.getId() || cusDevPlanMapper.selectByPrimaryKey(cusDevPlan.getId())==null,"数据异常，请重试！");
        checkCusDevPlanParams(cusDevPlan);

        /*2.设置修改时间*/
        cusDevPlan.setUpdateDate(new Date());

        /*3.执行更新操作，判断受影响的行数*/
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1,"计划更新失败，请重试！");
    }







    /**
     * 参数校验
     *      营销机会ID  非空，数据存在
     *      计划项内容   非空
     *      计划时间    非空
     * @param cusDevPlan
     */
    public void checkCusDevPlanParams(CusDevPlan cusDevPlan) {
        //营销机会ID  非空，数据存在
        Integer sId = cusDevPlan.getSaleChanceId();
        AssertUtil.isTrue(null == sId || saleChanceMapper.selectByPrimaryKey(sId) == null,"数据异常，请重试！");

        // 计划项内容   非空
        AssertUtil.isTrue(StringUtils.isBlank(cusDevPlan.getPlanItem()),"计划项内容不能为空！");

        //计划时间 非空
        AssertUtil.isTrue(null == cusDevPlan.getPlanDate(),"计划时间不能为空！");
    }


    /**
     * 删除计划项
     * @param id
     */
    public void deleteCusDevPlan(Integer id) {
        //先判断ID是否存在
        AssertUtil.isTrue(null == id ,"待删除记录不存在！");
        //通过ID查询计划项对象
        CusDevPlan cusDevPlan = cusDevPlanMapper.selectByPrimaryKey(id);
        //设置记录无效
        cusDevPlan.setIsValid(0);
        cusDevPlan.setUpdateDate(new Date());
        //执行更新
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan)!=1,"计划项数据删除失败!");

    }
}
